package com.ddwl.order.dao;

import com.alibaba.fastjson.JSON;
import com.ddwl.common.constant.LogisticsEnum;
import com.ddwl.common.constant.OrderEnum;
import com.ddwl.common.dao.TkDao;
import com.ddwl.common.exception.GlobalException;
import com.ddwl.common.util.BeanConverUtils;
import com.ddwl.common.util.MySqlUtil;
import com.ddwl.order.dao.mapper.OrderAddrMapper;
import com.ddwl.order.dao.model.OrderAddr;
import com.ddwl.order.dao.model.OrderInfo;
import com.ddwl.schema.bo.order.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;
import tk.mybatis.mapper.entity.Example;

import java.time.Instant;
import java.util.List;

/**
 * (OrderAddr)表数据库访问层
 *
 * @author makejava
 * @since 2020-06-02 14:51:53
 */
@Repository
@Slf4j
public class OrderAddrDao extends TkDao<OrderAddrMapper, OrderAddr> {

    /**
     * 查询订单信息
     *
     * @param orderId
     * @return
     */
    public List<OrderAddr> queryOrderAddrs(String orderId) {
        OrderAddr orderAddr = new OrderAddr();
        orderAddr.setOrderId(orderId);
        return baseMapper.select(orderAddr);
    }

    /**
     * 查询订单未发货的商品信息
     *
     * @param orderId
     * @return
     */
    public List<OrderAddr> queryOrderAddrsNotDeliver(String orderId) {
        OrderAddr orderAddr = new OrderAddr();
        orderAddr.setOrderId(orderId);
        orderAddr.setType(0);
        return baseMapper.select(orderAddr);
    }

    /**
     * 查询订单指定商品
     *
     * @param orderId
     * @param skuId
     * @return
     */
    public OrderAddr queryOrderAddrs(String orderId, String skuId) {
        OrderAddr orderAddr = new OrderAddr();
        orderAddr.setOrderId(orderId);
        orderAddr.setSkuId(skuId);
        return baseMapper.selectOne(orderAddr);
    }

    public List<OrderAddr> queryOrderAddrs(QueryOrderAdderListBo bo) {
        Example example = getExample();
        example.and().andLike("orderId", MySqlUtil.rightLike(bo.getOrderId()))
                .andLike("logisticsNo", MySqlUtil.rightLike(bo.getLogisticsNo()))
                .andLike("address", MySqlUtil.rightLike(bo.getAddress()))
                .andLike("mobile", MySqlUtil.rightLike(bo.getMobile()))
                .andLike("receiver", MySqlUtil.rightLike(bo.getReceiver()))
                .andEqualTo("regionId", bo.getRegionId())
                .andEqualTo("type", bo.getType())
                .andIsNotNull("payTime")
                .andEqualTo("logisticsName", bo.getLogisticsName())
                .andGreaterThanOrEqualTo("payTime", bo.getStartTime())
                .andLessThan("payTime", bo.getEndTime());
        return baseMapper.selectByExample(example);
    }

    public List<OrderAddr> queryApiOrderAddrs(OrderListApiBo bo) {
        Example example = getExample();
        example.and()
                .andEqualTo("status", bo.getStatus())
                .andNotEqualTo("status", 0)
                .andEqualTo("orderId", bo.getOrderId())
                .andIn("supplierId", bo.getSupplierList())
        ;
        if (bo.getOrderByModify() != null && bo.getOrderByModify()) {
            example.setOrderByClause("gmt_modified desc");
        } else {
            example.setOrderByClause("gmt_created desc");
        }
        return baseMapper.selectByExample(example);
    }

    public List<OrderAddr> queryApiOrderAddr(OrderListApiBo bo) {
        return baseMapper.queryApiOrderAddrs(bo);
    }

    public List<OrderAddr> queryOrderAddrs(OrderListApiBo bo) {
        return baseMapper.queryOrderAddrs(bo);
    }

    /**
     * 关闭订单
     *
     * @param orderNo
     */
    public void orderClosed(String orderNo) {
        OrderAddr orderAddr = new OrderAddr();
        orderAddr.setStatus(OrderEnum.OrderStatus.CLOSE.getCode());
        orderAddr.setGmtModified(Instant.now());
        Example example = new Example(OrderAddr.class);
        example.and().andEqualTo("orderId", orderNo)
                .andEqualTo("status", OrderEnum.OrderStatus.UN_PAY.getCode());
        Integer i = baseMapper.updateByExampleSelective(orderAddr, example);
        if (i < 1) {
            log.error("关闭订单失败 orderNo={}", JSON.toJSONString(orderNo));
            throw new GlobalException("关闭订单失败");
        }
    }


    public void orderDelivery(OrderDeliveryBo bo) {
        OrderAddr addr = new OrderAddr();
        addr.setDeliveryTime(Instant.now());
        addr.setLogisticsName(LogisticsEnum.LogisticsList.getValueByCode(bo.getLogisticsName()));
        addr.setLogisticsNo(bo.getLogisticsNo());
        addr.setLogisticsCode(bo.getLogisticsName());
        addr.setType(1);
        addr.setStatus(OrderEnum.OrderStatus.SEND.getCode());
        Example example = new Example(OrderAddr.class);
        example.and().andEqualTo("orderId", bo.getOrderId());
        Integer i = baseMapper.updateByExampleSelective(addr, example);
        if (i < 1) {
            log.error("订单发货异常 orderNo={}", JSON.toJSONString(bo.getOrderId()));
            throw new GlobalException("订单发货异常");
        }
    }


    /**
     * 根据订单ID更新发货信息
     */
    public void updateOrderAdderByOrderId(OrderAddr addr, String orderId) {
        Example example = new Example(OrderAddr.class);
        example.and().andEqualTo("orderId", orderId);
        baseMapper.updateByExampleSelective(addr, example);
    }

    public void orderApiDelivery(OrderDeliveryApiBo bo) {
        OrderAddr addr = new OrderAddr();
        addr.setDeliveryTime(Instant.now());
        addr.setLogisticsName(bo.getLogisticsName());
        addr.setLogisticsNo(bo.getLogisticsNo());
        addr.setLogisticsCode(bo.getLogisticsCode());
        addr.setType(1);
        addr.setStatus(OrderEnum.OrderStatus.SEND.getCode());
        Example example = new Example(OrderAddr.class);
        example.and().andEqualTo("orderId", bo.getOrderId());
        Integer i = baseMapper.updateByExampleSelective(addr, example);
        if (i < 1) {
            log.error("订单发货异常 orderNo={}", JSON.toJSONString(bo.getOrderId()));
            throw new GlobalException("订单发货异常");
        }
    }

    public void receive(OrderInfo bo) {
        OrderAddr addr = new OrderAddr();
        addr.setStatus(OrderEnum.OrderStatus.RECEIVED.getCode());
        addr.setDeliveryStatus(OrderEnum.DeliveryStatus.CONFIRM.getCode());
        addr.setConfirmTime(bo.getConfirmTime());
        Example example = new Example(OrderAddr.class);
        example.and().andEqualTo("orderId", bo.getId());
        Integer i = baseMapper.updateByExampleSelective(addr, example);
        if (i < 1) {
            log.error("订单收获异常 orderNo={}", JSON.toJSONString(bo.getId()));
            throw new GlobalException("订单收获异常");
        }
    }

    /**
     * 更新地址
     *
     * @param bo
     */
    public void updateAddr(OrderUpdateBo bo) {
        OrderAddr orderAddr = new OrderAddr();
        orderAddr = BeanConverUtils.convertBean(bo, OrderAddr.class, "orderId");
        orderAddr.setGmtModified(Instant.now());
        Example example = new Example(OrderInfo.class);
        example.and().andEqualTo("orderId", bo.getOrderId());
        Integer i = baseMapper.updateByExampleSelective(orderAddr, example);
        if (i < 1) {
            log.error("更新订单地址失败 orderNo={}", JSON.toJSONString(bo.getOrderId()));
            throw new GlobalException("更新订单备注失败");
        }
    }


    public List<OrderAddr> updateAddr() {
        Example example = getExample();
        example.and().andEqualTo("skuId", null)
        ;
        return baseMapper.selectByExample(example);
    }

    public void upateAdderByOrderId(OrderAddr orderAddr, String orderId) {
        Example example = new Example(OrderAddr.class);
        example.and().andEqualTo("orderId", orderId);
        baseMapper.updateByExampleSelective(orderAddr, example);
    }

}